package myList;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-01-27
 * Time: 10:51
 */
public class Test implements myList {
    private Integer[] array;
    private int size;
    private static final int DefaultCapacity = 10;

    public Test() {
        this.array = new Integer[DefaultCapacity];
    }

    public Test(int capacity) {
        this.array = new Integer[capacity];
    }

    private void checkCapacity() {
        // 判断是否满了
        if (isFull()) {
            this.array = Arrays.copyOf(array, array.length * 2);
        }
    }


    @Override
    public void add(int data) {
        // 判断数组是否需要扩容
        checkCapacity();
        // 添加到数组的最后一个位置
        this.array[this.size] = data;
        this.size++;
    }

    private void checkPosAddAndSet(int pos) throws PosillegalExeption {   // 声明异常
        if (pos < 0 || pos > this.size) {
            // 位置不合法
            System.out.println("坐标不合法!");
            throw new PosillegalExeption("非法坐标: " + pos);
        }
    }

    private void checkPosOnGet(int pos) throws PosillegalExeption {
        if (pos < 0 || pos >= this.size) {
            System.out.println("坐标不合法!");
            throw new PosillegalExeption("非法坐标: " + pos);
        }
    }

    @Override
    public void add(int pos, int data) {
        // 检查位置是否合法
        try {
            checkPosAddAndSet(pos);
        } catch (PosillegalExeption e) {
            e.printStackTrace();
            return;
        }
        // 检查是否需要扩容
        checkCapacity();

        // 将数据向后移动   i>=pos 继续
        for (int i = this.size - 1; i >= pos; i--) {
            this.array[i + 1] = this.array[i];
        }
        this.array[pos] = data;
        this.size++;

    }

    @Override
    public boolean contains(int toFind) {
        // 判断数组是否为空
        if (isEmpty()) {
            return false;
        }
        // 遍历寻找数组元素
        for (int i = 0; i < size; i++) {
            if (this.array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        // 判断数组是否为空
        if (isEmpty()) {
            return -1;
        }
        // 遍历寻找数组元素
        for (int i = 0; i < size; i++) {
            if (this.array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) throws PosillegalExeption {
        // 判断pos位置是否合法
        checkPosOnGet(pos);
        // 判断是否为空
        if (isEmpty()) {
            throw new ListEmptyExeption("下标位置为空pos: " + pos);
        }
        return this.array[pos];
    }

    @Override
    public void set(int pos, int value) throws PosillegalExeption {
        // 判断pos 位置是否合法
        checkPosAddAndSet(pos);
        if (isEmpty()) {
            throw new ListEmptyExeption("下标位置为空pos: " + pos);
        }

        this.array[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        // 先获取到这个元素的下标
        int index = indexOf(toRemove);
        if (index == -1) {
            System.out.println("没有这个元素!");
            return;
        }
        for (int i = index; i < this.size - 1; i++) {
            this.array[i] = this.array[i + 1];
        }
        this.size--;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public void clear() {
        this.size = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.size; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return this.size == this.array.length;
    }

    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }
}
